package de.wittekind.bjs.frontend.ticker;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import de.wittekind.bundesjugendspiele.server.impl.SQLRiegenLoader;
import de.wittekind.bundesjugendspiele.server.interfaces.Discipline;
import de.wittekind.bundesjugendspiele.server.interfaces.RiegenLoader;
import de.wittekind.bundesjugendspiele.server.interfaces.Schueler;
import de.wittekind.bundesjugendspiele.server.interfaces.Try;
import de.wittekind.bundesjugendspiele.server.mysqlTools.OpenShiftMySQLConnectionManager;


/**
 * Servlet implementation class LiveTicker
 */
@WebServlet("/LiveTicker")
public class LiveTicker extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private PrintWriter writer;
	private RiegenLoader riegenLoader;
	private static int s = 0;
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LiveTicker() {
		super();
		// 
	}

	
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		writer = response.getWriter();
		response.setContentType("text/html");
		
		
//		Registry registry = LocateRegistry.getRegistry(Constants.SERVER_HOST,Constants.REGISTRY_PORT);
//		
//		try {
//			riegenLoader = (RiegenLoader) registry.lookup(Constants.RMI_NAME);
//		} catch (NotBoundException e) {
//			// 
//			e.printStackTrace();
//		}
		try {
			OpenShiftMySQLConnectionManager.init();
			riegenLoader = new SQLRiegenLoader();
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		
		
		//writer.println(Discipline.WEITSPRUNG);
		//writer.println("<!DOCTYPE html>");
		writer.println("<html>");
		writer.println("<head>" +
				"<meta http-equiv=\"refresh\" content=\"10\"/>" +
				"<title>Liveticker Bundesjugendspiele 2013</title>" +
				"<link rel=\"stylesheet\" href=\"css/ticker.css\" type=\"text/css\"/>" +
				"</head>");
		
		writer.println("<body>");
		writer.println("<div id=\"header\"><div id=\"title\">");
		writer.println("<h1> Liveticker  </h1>");
		writer.println("</div></div><div id=\"container\">");
		writer.println("<h2> Bundesjugendspiele 2013  </h2>");
		writer.println("<br/>");

		
	
		for(Discipline discipline:Discipline.values())
		{
			writer.println("<div class=\"content\" >");
			showDiscipline(discipline);
			writer.println("</div>");
			
		}
		
		writer.println("<br/>");
		
		writer.println("</div>");
		
		writer.println("</body>");
		writer.println("</html>");

		writer.close();
	}
	
	public void showDiscipline (Discipline discipline) throws IOException{
		int year=getYear();
		Map<Schueler, Try> values = riegenLoader.getBestResults(5, discipline, year);
		Set<Schueler> schueler = values.keySet();
		writer.println("<h3> "+ discipline +" </h3>");
		writer.println("<table class=\"table\">");
		writer.println("<tr><th>"+discipline+"</th><th>Altersklasse "+year+"</th></tr>");
		for (Schueler s : schueler) {
			String sName = s.getFirstName() + " " + s.getLastName();
			Try t = values.get(s);
			String sValue = new Double(t.getValue()).toString() + t.getUnit();
			
			writer.println("<tr>");
			writer.println("<td>" + sName + "</td>");
			//String sValue;
			writer.println("<td>" + sValue  + "</td>");
			writer.println("</tr>");
		}
		
		
		
		writer.println("</table>");
	}
	
	public int getYear() throws IOException{
		int year = 0;
		year = riegenLoader.getMinJahrgang();
		year = year + s;
		if(year==riegenLoader.getMaxJahrgang()){
		s = 0;
		}
		else{
		s++;
		}
		return year;
	}
}
